// ==UserScript==
// @name         网站URL简化|去除杂乱参数
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  自动清理必应搜索、B站视频、百度搜索、KIMI AI和360搜索等的URL中的多余参数，优化浏览体验
// @author       xjy666a
// @license      MIT
// @match        https://cn.bing.com/search*
// @match        https://www.bing.com/search*
// @match        https://www.bilibili.com/video/*
// @match        https://www.baidu.com/*
// @match        https://kimi.moonshot.cn/*
// @match        https://minecraft.fandom.com/*
// @match        https://www.so.com/s*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_setClipboard
// @grant        unsafeWindow
// @grant        GM_xmlhttpRequest
// @run-at       document-start
// @priority     1
// @icon         https://www.helloimg.com/i/2025/04/26/680c9e8d2db2f.png
// @downloadURL https://update.greasyfork.org.cn/scripts/524527/%E7%BD%91%E7%AB%99URL%E7%AE%80%E5%8C%96%7C%E5%8E%BB%E9%99%A4%E6%9D%82%E4%B9%B1%E5%8F%82%E6%95%B0.user.js
// @updateURL https://update.greasyfork.org.cn/scripts/524527/%E7%BD%91%E7%AB%99URL%E7%AE%80%E5%8C%96%7C%E5%8E%BB%E9%99%A4%E6%9D%82%E4%B9%B1%E5%8F%82%E6%95%B0.meta.js
// ==/UserScript==

/* MIT License

Copyright (c) 2024 xjy666a

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

(function() {
    'use strict';

    // 默认设置
    const defaultSettings = {
        enableCleaner: true,
        enableBing: true,
        enableBilibili: true,
        enableBaidu: true,
        enableKimi: true,
        enableMinecraft: true,
        enable360: true,
        enableClipboardCleaner: true,
        usageCount: 0,
        ratingRequested: false
    };

    // 获取设置
    function getSettings() {
        return {
            enableCleaner: GM_getValue('enableCleaner', defaultSettings.enableCleaner),
            enableBing: GM_getValue('enableBing', defaultSettings.enableBing),
            enableBilibili: GM_getValue('enableBilibili', defaultSettings.enableBilibili),
            enableBaidu: GM_getValue('enableBaidu', defaultSettings.enableBaidu),
            enableKimi: GM_getValue('enableKimi', defaultSettings.enableKimi),
            enableMinecraft: GM_getValue('enableMinecraft', defaultSettings.enableMinecraft),
            enable360: GM_getValue('enable360', defaultSettings.enable360),
            enableClipboardCleaner: GM_getValue('enableClipboardCleaner', defaultSettings.enableClipboardCleaner),
            usageCount: GM_getValue('usageCount', 0),
            ratingRequested: GM_getValue('ratingRequested', false)
        };
    }

    // 切换设置并返回新状态
    function toggleSetting(key) {
        const currentValue = GM_getValue(key, defaultSettings[key]);
        GM_setValue(key, !currentValue);
        return !currentValue;
    }

    // 注册菜单命令
    function registerMenuCommands() {
        const settings = getSettings();

        // 主菜单项
        GM_registerMenuCommand(
            `📊 使用次数: ${settings.usageCount} 次`,
            () => showUsageStats()
        );

        GM_registerMenuCommand(
            `💬 提供反馈或建议`,
            () => showFeedbackPrompt()
        );

        GM_registerMenuCommand(
            `${settings.enableCleaner ? '✅' : '❌'} 启用URL清理`,
            () => {
                toggleSetting('enableCleaner');
                location.reload();
            }
        );

        // 网站设置子菜单
        GM_registerMenuCommand(
            `🔧 网站设置...`,
            () => showWebsiteSettings()
        );

        GM_registerMenuCommand(
            `📅 显示信息面板`,
            () => showInfoOverlay()
        );
    }

    // 显示使用统计详情
    function showUsageStats() {
        const settings = getSettings();

        // 创建统计信息弹窗
        const statsPrompt = document.createElement('div');
        statsPrompt.className = 'usage-stats-prompt';
        statsPrompt.style.cssText = `
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: rgba(34, 34, 34, 0.95);
            color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
            z-index: 10001;
            font-size: 14px;
            min-width: 300px;
            text-align: center;
            transition: opacity 0.3s;
        `;

        // 计算使用天数（假设脚本安装日期存储在installDate中）
        const installDate = GM_getValue('installDate', Date.now());
        const daysUsed = Math.ceil((Date.now() - installDate) / (1000 * 60 * 60 * 24));

        // 弹窗内容
        statsPrompt.innerHTML = `
            <div style="margin-bottom: 15px; font-weight: bold; font-size: 16px;">URL简化脚本使用统计</div>
            <div style="margin-bottom: 10px; text-align: left; padding: 0 10px;">
                <div style="display: flex; justify-content: space-between; margin-bottom: 8px;">
                    <span>总使用次数:</span>
                    <span style="font-weight: bold; color: #4CAF50;">${settings.usageCount} 次</span>
                </div>
                <div style="display: flex; justify-content: space-between; margin-bottom: 8px;">
                    <span>已使用天数:</span>
                    <span style="font-weight: bold; color: #2196F3;">${daysUsed} 天</span>
                </div>
                <div style="display: flex; justify-content: space-between; margin-bottom: 8px;">
                    <span>平均每天使用:</span>
                    <span style="font-weight: bold; color: #FFC107;">${(settings.usageCount / Math.max(daysUsed, 1)).toFixed(1)} 次</span>
                </div>
            </div>
            <button class="close-stats-prompt" style="
                background-color: #555;
                color: white;
                border: none;
                padding: 8px 15px;
                border-radius: 4px;
                cursor: pointer;
                margin-top: 10px;
                font-weight: bold;
            ">关闭</button>
        `;

        // 添加到页面
        document.body.appendChild(statsPrompt);

        // 关闭按钮点击事件
        statsPrompt.querySelector('.close-stats-prompt').addEventListener('click', function() {
            document.body.removeChild(statsPrompt);
        });
    }

    // 显示反馈弹窗
    function showFeedbackPrompt() {
        // 检查是否已存在弹窗
        if (document.querySelector('.feedback-prompt')) {
            return;
        }

        // 创建弹窗
        const prompt = document.createElement('div');
        prompt.className = 'feedback-prompt';
        prompt.style.cssText = `
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: rgba(34, 34, 34, 0.95);
            color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
            z-index: 10001;
            font-size: 14px;
            max-width: 400px;
            text-align: center;
            transition: opacity 0.3s;
        `;

        // 弹窗内容
        prompt.innerHTML = `
            <div style="margin-bottom: 15px; font-weight: bold; font-size: 16px;">功能反馈与建议</div>
            <div style="margin-bottom: 20px; line-height: 1.5; text-align: left;">
                目前本脚本功能较少，你可以反馈，若可以实现，我们会尽量满足！
            </div>
            <div style="display: flex; justify-content: center; margin-bottom: 15px;">
                <a href="https://scriptcat.org/zh-CN/script-show-page/2654/" target="_blank" style="
                    display: inline-block;
                    background-color: #4CAF50;
                    color: white;
                    text-decoration: none;
                    padding: 8px 15px;
                    border-radius: 4px;
                    margin-right: 10px;
                    font-weight: bold;
                ">在脚本猫反馈</a>
                <a href="https://greasyfork.org.cn/zh-CN/scripts/524527-网站url简化-去除杂乱参数" target="_blank" style="
                    display: inline-block;
                    background-color: #2196F3;
                    color: white;
                    text-decoration: none;
                    padding: 8px 15px;
                    border-radius: 4px;
                    font-weight: bold;
                ">在Greasy Fork反馈</a>
            </div>
            <button class="close-feedback-prompt" style="
                background-color: transparent;
                color: #ddd;
                border: 1px solid #666;
                padding: 5px 10px;
                border-radius: 4px;
                cursor: pointer;
                margin-top: 5px;
            ">关闭</button>
        `;

        // 添加到页面
        document.body.appendChild(prompt);

        // 关闭按钮点击事件
        prompt.querySelector('.close-feedback-prompt').addEventListener('click', function() {
            document.body.removeChild(prompt);
        });

        // 点击反馈链接时关闭弹窗
        prompt.querySelectorAll('a').forEach(link => {
            link.addEventListener('click', function() {
                setTimeout(() => {
                    if (document.body.contains(prompt)) {
                        document.body.removeChild(prompt);
                    }
                }, 500);
            });
        });
    }

    // 显示网站设置弹窗
    function showWebsiteSettings() {
        const settings = getSettings();

        // 创建设置弹窗
        const settingsPrompt = document.createElement('div');
        settingsPrompt.className = 'website-settings-prompt';
        settingsPrompt.style.cssText = `
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: rgba(34, 34, 34, 0.95);
            color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
            z-index: 10001;
            font-size: 14px;
            min-width: 300px;
            max-width: 500px;
            max-height: 80vh;
            overflow-y: auto;
            text-align: left;
            transition: opacity 0.3s;
        `;

        // 网站设置列表
        const websiteSettings = [
            { key: 'enableBing', name: '必应搜索', icon: '🔍' },
            { key: 'enableBilibili', name: 'B站视频', icon: '📺' },
            { key: 'enableBaidu', name: '百度搜索', icon: '🔍' },
            { key: 'enableKimi', name: 'KIMI AI', icon: '🤖' },
            { key: 'enableMinecraft', name: 'Minecraft Wiki重定向', icon: '🎮' },
            { key: 'enable360', name: '360搜索', icon: '🔍' },
            { key: 'enableClipboardCleaner', name: 'B站分享链接自动清理', icon: '��' }
            // 未来可以在这里添加更多网站
        ];

        // 构建设置项HTML
        let settingsHTML = `
            <div style="margin-bottom: 15px; font-weight: bold; font-size: 16px; text-align: center;">网站设置</div>
            <div style="margin-bottom: 15px;">
                启用或禁用特定网站的URL清理功能:
            </div>
            <div style="display: grid; grid-template-columns: auto 1fr; gap: 10px; align-items: center;">
        `;

        // 添加每个网站的设置项
        websiteSettings.forEach(site => {
            settingsHTML += `
                <div style="display: flex; align-items: center;">
                    <span style="margin-right: 8px;">${site.icon}</span>
                    <span>${site.name}</span>
                </div>
                <label class="switch" style="justify-self: end;">
                    <input type="checkbox" data-key="${site.key}" ${settings[site.key] ? 'checked' : ''}>
                    <span class="slider" style="
                        position: relative;
                        display: inline-block;
                        width: 40px;
                        height: 20px;
                        background-color: ${settings[site.key] ? '#4CAF50' : '#ccc'};
                        border-radius: 10px;
                        transition: .4s;
                        cursor: pointer;
                    "></span>
                </label>
            `;
        });

        // 添加按钮
        settingsHTML += `
            </div>
            <div style="display: flex; justify-content: center; margin-top: 20px;">
                <button class="save-settings" style="
                    background-color: #4CAF50;
                    color: white;
                    border: none;
                    padding: 8px 15px;
                    border-radius: 4px;
                    cursor: pointer;
                    margin-right: 10px;
                    font-weight: bold;
                ">保存设置</button>
                <button class="close-settings" style="
                    background-color: #555;
                    color: white;
                    border: none;
                    padding: 8px 15px;
                    border-radius: 4px;
                    cursor: pointer;
                    font-weight: bold;
                ">取消</button>
            </div>
        `;

        // 设置弹窗内容
        settingsPrompt.innerHTML = settingsHTML;

        // 添加到页面
        document.body.appendChild(settingsPrompt);

        // 切换开关样式
        settingsPrompt.querySelectorAll('input[type="checkbox"]').forEach(checkbox => {
            checkbox.addEventListener('change', function() {
                const slider = this.nextElementSibling;
                slider.style.backgroundColor = this.checked ? '#4CAF50' : '#ccc';
            });
        });

        // 保存按钮点击事件
        settingsPrompt.querySelector('.save-settings').addEventListener('click', function() {
            // 保存所有设置
            settingsPrompt.querySelectorAll('input[type="checkbox"]').forEach(checkbox => {
                const key = checkbox.dataset.key;
                GM_setValue(key, checkbox.checked);
            });

            // 显示保存成功通知
            showNotification('设置已保存');

            // 关闭弹窗
            document.body.removeChild(settingsPrompt);

            // 重新加载页面以应用设置
            location.reload();
        });

        // 关闭按钮点击事件
        settingsPrompt.querySelector('.close-settings').addEventListener('click', function() {
            document.body.removeChild(settingsPrompt);
        });
    }

    // 增加使用计数并检查是否需要请求评分
    function incrementUsageCount() {
        const settings = getSettings();

        // 增加使用计数
        const newCount = settings.usageCount + 1;
        GM_setValue('usageCount', newCount);

        // 检查是否需要请求评分 - 将阈值从50改为10
        if (newCount >= 10 && !settings.ratingRequested) {
            // 显示评分请求
            showRatingPrompt();
            // 标记已请求评分
            GM_setValue('ratingRequested', true);
        }
    }

    // 显示评分请求提示
    function showRatingPrompt() {
        // 检查是否已存在提示框
        if (document.querySelector('.rating-prompt')) {
            return;
        }

        // 创建提示框
        const prompt = document.createElement('div');
        prompt.className = 'rating-prompt';
        prompt.style.cssText = `
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: rgba(34, 34, 34, 0.95);
            color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
            z-index: 10001;
            font-size: 14px;
            max-width: 400px;
            text-align: center;
            transition: opacity 0.3s;
        `;

        // 提示框内容 - 修改文本以反映新的阈值
        prompt.innerHTML = `
            <div style="margin-bottom: 15px; font-weight: bold; font-size: 16px;">感谢您使用URL简化脚本！</div>
            <div style="margin-bottom: 20px; line-height: 1.5;">
                您已经使用本脚本超过10次，如果觉得它对您有帮助，希望您能花一点时间给它评个分，这将帮助更多人发现它。
            </div>
            <div style="display: flex; justify-content: center; margin-bottom: 15px;">
                <a href="https://scriptcat.org/zh-CN/script-show-page/2654/" target="_blank" style="
                    display: inline-block;
                    background-color: #4CAF50;
                    color: white;
                    text-decoration: none;
                    padding: 8px 15px;
                    border-radius: 4px;
                    margin-right: 10px;
                    font-weight: bold;
                ">在脚本猫评分</a>
                <a href="https://greasyfork.org.cn/zh-CN/scripts/524527-网站url简化-去除杂乱参数" target="_blank" style="
                    display: inline-block;
                    background-color: #2196F3;
                    color: white;
                    text-decoration: none;
                    padding: 8px 15px;
                    border-radius: 4px;
                    font-weight: bold;
                ">在Greasy Fork评分</a>
            </div>
            <button class="close-rating-prompt" style="
                background-color: transparent;
                color: #ddd;
                border: 1px solid #666;
                padding: 5px 10px;
                border-radius: 4px;
                cursor: pointer;
                margin-top: 5px;
            ">稍后再说</button>
            <div style="font-size: 12px; margin-top: 15px; color: #aaa;">
                您的支持是我们持续改进的动力！
            </div>
        `;

        // 添加到页面
        document.body.appendChild(prompt);

        // 关闭按钮点击事件
        prompt.querySelector('.close-rating-prompt').addEventListener('click', function() {
            document.body.removeChild(prompt);
        });

        // 点击评分链接时关闭提示框
        prompt.querySelectorAll('a').forEach(link => {
            link.addEventListener('click', function() {
                // 标记用户已点击评分链接
                GM_setValue('userRated', true);
                setTimeout(() => {
                    if (document.body.contains(prompt)) {
                        document.body.removeChild(prompt);
                    }
                }, 500);
            });
        });
    }

    // 清理URL的函数
    function cleanUrl(url) {
        try {
            const settings = getSettings();
            if (!settings.enableCleaner) {
                return url;
            }

            // 增加使用计数
            incrementUsageCount();

            const urlObj = new URL(url);

            // 处理Minecraft Wiki重定向
            if (settings.enableMinecraft && urlObj.hostname === 'minecraft.fandom.com') {
                const pathParts = urlObj.pathname.split('/');
                let newUrl;

                if (pathParts[1] === 'wiki') {
                    const pageName = pathParts.slice(2).join('/');
                    newUrl = `https://en.minecraft.wiki/w/${pageName}`;
                } else if (pathParts[2] === 'wiki') {
                    const lang = pathParts[1];
                    const pageName = pathParts.slice(3).join('/');
                    newUrl = `https://${lang}.minecraft.wiki/w/${pageName}`;
                }

                if (newUrl && newUrl !== url) {
                    window.location.href = newUrl;
                    return url;
                }
            }

            // 处理KIMI AI URL
            if (settings.enableKimi && urlObj.hostname === 'kimi.moonshot.cn') {
                if (urlObj.pathname === '/' || urlObj.pathname === '') {
                    const newUrl = 'https://kimi.moonshot.cn/';
                    if (newUrl !== url) {
                        window.location.href = newUrl;
                        return url;
                    }
                    return newUrl;
                }
            }

            // 处理百度搜索URL（已修复空格编码）
            if (settings.enableBaidu && urlObj.hostname === 'www.baidu.com' && urlObj.pathname === '/s') {
                const wd = urlObj.searchParams.get('wd');
                const pn = urlObj.searchParams.get('pn');

                if (wd) {
                    let newUrl = `https://www.baidu.com/s?wd=${encodeURIComponent(wd).replace(/%20/g, '+')}`;
                    if (pn) {
                        newUrl += `&pn=${pn}`;
                    }
                    if (newUrl !== url) {
                        window.location.href = newUrl;
                        return url;
                    }
                    return newUrl;
                }
            }

            // 处理Bing搜索URL (包括国际版)
            if (settings.enableBing && (urlObj.hostname === 'cn.bing.com' || urlObj.hostname === 'www.bing.com') && urlObj.pathname === '/search') {
                const searchQuery = urlObj.searchParams.get('q');
                const firstParam = urlObj.searchParams.get('first');

                if (searchQuery) {
                    // 保持原始域名不变
                    let newUrl = `https://${urlObj.hostname}/search?q=${encodeURIComponent(searchQuery)}`;
                    if (firstParam) {
                        newUrl += `&first=${firstParam}`;
                    }
                    if (newUrl !== url) {
                        window.location.href = newUrl;
                        return url;
                    }
                    return newUrl;
                }
            }

            // 处理B站视频URL
            if (settings.enableBilibili && urlObj.hostname === 'www.bilibili.com' && urlObj.pathname.startsWith('/video/')) {
                const bvMatch = urlObj.pathname.match(/\/video\/(BV[\w]+)/);
                if (bvMatch) {
                    const bvid = bvMatch[1];
                    const newUrl = `https://www.bilibili.com/video/${bvid}`;
                    // 只返回新URL，不进行跳转
                    return newUrl;
                }
            }

            // 处理360搜索URL
            if (settings.enable360 && urlObj.hostname === 'www.so.com' && urlObj.pathname === '/s') {
                const q = urlObj.searchParams.get('q');
                const pn = urlObj.searchParams.get('pn');

                if (q) {
                    let newUrl = `https://www.so.com/s?q=${encodeURIComponent(q)}`;
                    if (pn) {
                        newUrl += `&pn=${pn}`;
                    }
                    if (newUrl !== url) {
                        window.location.href = newUrl;
                        return url;
                    }
                    return newUrl;
                }
            }

            return url;
        } catch (error) {
            console.error('URL处理错误:', error);
            return url;
        }
    }

    // 检查并清理当前URL
    function checkAndCleanUrl() {
        const currentUrl = window.location.href;
        const cleanedUrl = cleanUrl(currentUrl);

        if (cleanedUrl !== currentUrl) {
            // 使用 history.replaceState 来更新URL而不刷新页面
            window.history.replaceState(null, '', cleanedUrl);
        }
    }

    // 监听URL变化
    let lastUrl = window.location.href;
    new MutationObserver(() => {
        const currentUrl = window.location.href;
        if (currentUrl !== lastUrl) {
            lastUrl = currentUrl;
            checkAndCleanUrl();
        }
    }).observe(document, {subtree: true, childList: true});

    // 处理必应搜索结果中的Minecraft Wiki链接
    function processBingSearchResults() {
        // 同时支持中国版和国际版必应
        if (!window.location.href.includes('.bing.com/search')) return;

        // 检查页面是否有内容
        const mainResults = document.getElementById('b_results') ||
                           document.querySelector('.b_results') ||
                           document.querySelector('#main');

        // 修改判断条件：检查搜索结果是否存在且内容是否足够
        if (!mainResults || mainResults.children.length < 2) {
            console.log('必应搜索结果似乎为空，准备重试...');

            // 重试机制
            if (typeof window.bingRetryCount === 'undefined') {
                window.bingRetryCount = 0;
            }

            if (window.bingRetryCount < 3) {
                window.bingRetryCount++;
                console.log(`重试第 ${window.bingRetryCount} 次...`);

                // 延迟2秒后重试，给予更多加载时间
                setTimeout(() => {
                    // 如果已经重试了但还是没有结果，保留参数重新加载
                    if (window.bingRetryCount >= 2) {
                        console.log('尝试保留参数重新加载...');
                        sessionStorage.setItem('cleanUrlAfterLoad', 'true');
                        window.location.reload(true); // 强制从服务器重新加载
                    } else {
                        window.location.reload();
                    }
                }, 2000);

                return;
            } else {
                console.log('已达到最大重试次数，保留参数加载页面');
                // 标记为已处理，避免无限循环
                window.bingRetryHandled = true;

                // 获取当前URL并保留所有参数
                const currentUrl = window.location.href;

                // 设置一个标记，表示页面已经加载完成后再清理URL
                sessionStorage.setItem('cleanUrlAfterLoad', 'true');
                sessionStorage.setItem('originalUrl', currentUrl);

                // 不再尝试清理URL，让页面正常加载
                return;
            }
        } else {
            // 如果页面加载成功，重置计数器
            window.bingRetryCount = 0;

            // 检查是否需要在页面加载后清理URL
            if (sessionStorage.getItem('cleanUrlAfterLoad') === 'true') {
                const originalUrl = sessionStorage.getItem('originalUrl');
                sessionStorage.removeItem('cleanUrlAfterLoad');
                sessionStorage.removeItem('originalUrl');

                // 延迟执行URL清理，确保页面已完全加载
                setTimeout(() => {
                    if (mainResults && mainResults.children.length > 2) {
                        checkAndCleanUrl();
                    }
                }, 2000);
            }
        }

        // 获取所有未处理的搜索结果链接
        const searchResults = mainResults.querySelectorAll('a[href*="minecraft.fandom.com"]:not([data-wiki-processed])');

        searchResults.forEach(link => {
            try {
                // 标记该链接已处理
                link.setAttribute('data-wiki-processed', 'true');

                const url = new URL(link.href);
                if (url.hostname === 'minecraft.fandom.com') {
                    const pathParts = url.pathname.split('/');
                    let newUrl;

                    // 构建新的Wiki URL
                    if (pathParts[1] === 'wiki') {
                        const pageName = pathParts.slice(2).join('/');
                        newUrl = `https://en.minecraft.wiki/w/${pageName}`;
                    } else if (pathParts[2] === 'wiki') {
                        const lang = pathParts[1];
                        const pageName = pathParts.slice(3).join('/');
                        newUrl = `https://${lang}.minecraft.wiki/w/${pageName}`;
                    }

                    if (newUrl) {
                        // 获取搜索结果容器
                        const resultContainer = link.closest('li') || link.parentElement;

                        // 设置结果容器样式
                        resultContainer.style.position = 'relative';
                        resultContainer.style.color = '#666';
                        resultContainer.style.pointerEvents = 'none';

                        // 创建新链接提示
                        const notice = document.createElement('div');
                        notice.style.cssText = `
                            margin-top: 8px;
                            padding: 8px;
                            background: #f8f8f8;
                            border-radius: 4px;
                            pointer-events: auto;
                        `;
                        notice.innerHTML = `
                            <div style="color: #e74c3c; font-size: 0.9em; margin-bottom: 4px;">
                                ⚠️ 上述链接指向已弃用的旧版Wiki
                            </div>
                            <a href="${newUrl}" style="
                                display: inline-block;
                                color: #2ecc71;
                                font-weight: bold;
                                text-decoration: none;
                            ">
                                👉 访问新版Wiki页面
                            </a>
                        `;

                        // 添加新链接提示
                        resultContainer.appendChild(notice);
                    }
                }
            } catch (error) {
                console.error('处理搜索结果链接时出错:', error);
            }
        });
    }

    // 使用防抖函数来限制处理频率
    function debounce(func, wait) {
        let timeout;
        return function executedFunction(...args) {
            const later = () => {
                clearTimeout(timeout);
                func(...args);
            };
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
        };
    }

    // 监听页面变化以处理动态加载的搜索结果
    function observeSearchResults() {
        const debouncedProcess = debounce(processBingSearchResults, 300);

        // 创建观察器
        const observer = new MutationObserver(() => {
            // 兼容不同版本的必应
            if (document.getElementById('b_results') ||
                document.querySelector('.b_results') ||
                document.querySelector('#main')) {
                debouncedProcess();
            }
        });

        // 观察整个body
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });

        // 首次处理
        processBingSearchResults();

        // 监听URL变化
        let lastUrl = location.href;
        const urlChecker = setInterval(() => {
            if (location.href !== lastUrl) {
                lastUrl = location.href;
                // 重置重试计数器
                window.bingRetryCount = 0;
                processBingSearchResults();
            }
        }, 500);

        // 清理函数
        return () => {
            observer.disconnect();
            clearInterval(urlChecker);
        };
    }

    // 修改B站分享链接清理函数
    function cleanBilibiliShareLink(text) {
        // 检查是否包含B站视频链接
        if (!text.includes('bilibili.com/video/BV')) {
            return text;
        }

        try {
            // 检查是否已经是清理过的链接（被||包围的标题）
            if (text.match(/\|\|.+?\|\|\s+https:\/\/www\.bilibili\.com\/video\/BV[\w]+\//)) {
                return text;
            }

            // 提取BV号
            const bvMatch = text.match(/bilibili\.com\/video\/(BV[\w]+)/);
            if (!bvMatch) return text;

            const bvid = bvMatch[1];

            // 检查是否有标题格式【标题】
            const titleMatch = text.match(/【(.+?)】/);
            const title = titleMatch ? titleMatch[1] : '';

            // 构建清理后的链接
            const cleanedUrl = `https://www.bilibili.com/video/${bvid}/`;

            // 返回清理后的完整文本，使用||包围标题
            if (title) {
                return `||${title}|| ${cleanedUrl}`;
            } else {
                return cleanedUrl;
            }
        } catch (error) {
            console.error('清理B站分享链接时出错:', error);
            return text;
        }
    }

    // B站专用剪贴板监听函数
    function monitorBilibiliClipboard() {
        // 只在B站页面上运行
        if (!window.location.hostname.includes('bilibili.com')) return;

        const settings = getSettings();
        if (!settings.enableClipboardCleaner || !settings.enableBilibili) return;

        // 存储已处理的链接，避免重复处理
        const processedLinks = new Set();

        // 定期检查剪贴板内容
        const clipboardCheckInterval = setInterval(() => {
            navigator.clipboard.readText().then(text => {
                // 如果文本已经是清理过的格式（被||包围的标题），跳过
                if (text.match(/\|\|.+?\|\|\s+https:\/\/www\.bilibili\.com\/video\/BV[\w]+\//)) {
                    return;
                }

                // 检查是否是B站链接且包含参数
                if (text && text.includes('bilibili.com/video/BV') && text.includes('?')) {
                    // 生成唯一标识，避免重复处理相同链接
                    const linkId = text.trim();

                    // 如果已经处理过这个链接，跳过
                    if (processedLinks.has(linkId)) return;

                    // 添加到已处理集合
                    processedLinks.add(linkId);

                    // 清理链接
                    const cleanedText = cleanBilibiliShareLink(text);

                    // 如果清理后有变化，显示提示
                    if (cleanedText !== text) {
                        // 增加使用计数
                        incrementUsageCount();

                        // 显示提示框让用户选择复制简化链接
                        showCleanLinkPrompt(cleanedText);
                    }

                    // 限制已处理链接集合大小，避免内存泄漏
                    if (processedLinks.size > 50) {
                        const iterator = processedLinks.values();
                        processedLinks.delete(iterator.next().value);
                    }
                }
            }).catch(err => {
                console.error('读取剪贴板失败:', err);
            });
        }, 1000); // 每秒检查一次

        // 页面卸载时清除定时器
        window.addEventListener('unload', () => {
            clearInterval(clipboardCheckInterval);
        });

        // 仍然保留复制事件监听，以便更及时地响应
        document.addEventListener('copy', function() {
            setTimeout(() => {
                navigator.clipboard.readText().then(text => {
                    // 如果文本已经是清理过的格式，跳过
                    if (text.match(/\|\|.+?\|\|\s+https:\/\/www\.bilibili\.com\/video\/BV[\w]+\//)) {
                        return;
                    }

                    if (text && text.includes('bilibili.com/video/BV') && text.includes('?')) {
                        const linkId = text.trim();
                        if (processedLinks.has(linkId)) return;

                        processedLinks.add(linkId);
                        const cleanedText = cleanBilibiliShareLink(text);
                        if (cleanedText !== text) {
                            // 增加使用计数
                            incrementUsageCount();

                            // 显示提示框让用户选择复制简化链接
                            showCleanLinkPrompt(cleanedText);
                        }
                    }
                }).catch(err => console.error('读取剪贴板失败:', err));
            }, 200);
        });
    }

    // 显示清理链接提示框
    function showCleanLinkPrompt(cleanedText) {
        // 检查是否已存在提示框，避免重复显示
        if (document.querySelector('.clean-link-prompt')) {
            return;
        }

        // 创建提示框
        const prompt = document.createElement('div');
        prompt.className = 'clean-link-prompt';
        prompt.style.cssText = `
            position: fixed;
            bottom: 20px;
            right: 20px;
            background-color: rgba(34, 34, 34, 0.9);
            color: white;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
            z-index: 10000;
            font-size: 14px;
            max-width: 300px;
            transition: opacity 0.3s;
        `;

        // 提示框内容
        prompt.innerHTML = `
            <div style="margin-bottom: 10px; font-weight: bold;">检测到B站分享链接</div>
            <div style="margin-bottom: 12px; color: #ddd; font-size: 12px; word-break: break-all;">${cleanedText}</div>
            <button class="copy-clean-link" style="
                background-color: #00a1d6;
                color: white;
                border: none;
                padding: 5px 10px;
                border-radius: 4px;
                cursor: pointer;
                margin-right: 10px;
            ">复制简化链接</button>
            <button class="close-prompt" style="
                background-color: transparent;
                color: #ddd;
                border: 1px solid #666;
                padding: 5px 10px;
                border-radius: 4px;
                cursor: pointer;
            ">关闭</button>
        `;

        // 添加到页面
        document.body.appendChild(prompt);

        // 复制按钮点击事件
        prompt.querySelector('.copy-clean-link').addEventListener('click', function() {
            GM_setClipboard(cleanedText);
            showNotification('已复制简化链接');
            document.body.removeChild(prompt);
        });

        // 关闭按钮点击事件
        prompt.querySelector('.close-prompt').addEventListener('click', function() {
            document.body.removeChild(prompt);
        });

        // 10秒后自动关闭
        setTimeout(() => {
            if (document.body.contains(prompt)) {
                prompt.style.opacity = '0';
                setTimeout(() => {
                    if (document.body.contains(prompt)) {
                        document.body.removeChild(prompt);
                    }
                }, 300);
            }
        }, 10000);
    }

    // 显示通知
    function showNotification(message) {
        // 创建通知元素
        const notification = document.createElement('div');
        notification.textContent = message;
        notification.style.cssText = `
            position: fixed;
            bottom: 20px;
            right: 20px;
            background-color: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 10px 15px;
            border-radius: 4px;
            z-index: 9999;
            font-size: 14px;
            transition: opacity 0.3s;
        `;

        // 添加到页面
        document.body.appendChild(notification);

        // 2秒后淡出
        setTimeout(() => {
            notification.style.opacity = '0';
            setTimeout(() => {
                document.body.removeChild(notification);
            }, 300);
        }, 2000);
    }

    // 初始化
    function init() {
        // 记录安装日期（如果尚未记录）
        if (!GM_getValue('installDate')) {
            GM_setValue('installDate', Date.now());
        }

        // 检查是否是从重试后的页面加载
        const needCleanAfterLoad = sessionStorage.getItem('cleanUrlAfterLoad') === 'true';

        // 如果不是重试后的页面加载，正常注册菜单和清理URL
        if (!needCleanAfterLoad) {
            registerMenuCommands();
            checkAndCleanUrl();
        } else {
            // 如果是重试后的页面加载，只注册菜单，不立即清理URL
            registerMenuCommands();
            console.log('页面通过保留参数加载，将在加载完成后清理URL');
        }

        // 重置必应重试计数器
        window.bingRetryCount = 0;
        window.bingRetryHandled = false;

        // 如果是必应搜索页面(包括国际版)，处理搜索结果
        if (window.location.href.includes('.bing.com/search')) {
            if (document.readyState === 'loading') {
                document.addEventListener('DOMContentLoaded', observeSearchResults);
            } else {
                observeSearchResults();
            }
        }

        // 设置B站专用剪贴板监听
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', monitorBilibiliClipboard);
        } else {
            monitorBilibiliClipboard();
        }

        // 添加KIMI AI公式处理
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', setupKimiFormulaHandler);
        } else {
            setupKimiFormulaHandler();
        }
    }

    // 设置KIMI AI公式处理
    function setupKimiFormulaHandler() {
        const settings = getSettings();
        if (!settings.enableKimi || !settings.enableCleaner) return;

        // 只在KIMI AI页面上运行
        if (!window.location.hostname.includes('kimi.moonshot.cn')) return;

        // 使用MutationObserver监听DOM变化，处理动态加载的公式
        const observer = new MutationObserver(debounce(() => {
            addFormulaClickHandlers();
        }, 500));

        // 观察整个body的变化
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });

        // 首次运行
        addFormulaClickHandlers();

        // 页面卸载时清除观察器
        window.addEventListener('unload', () => {
            observer.disconnect();
        });
    }

    // 为公式添加点击处理器
    function addFormulaClickHandlers() {
        // 查找所有KaTeX公式元素
        const formulas = document.querySelectorAll('.katex-html:not([data-formula-processed])');

        formulas.forEach(formula => {
            // 标记为已处理
            formula.setAttribute('data-formula-processed', 'true');

            // 获取公式的父元素，使整个公式可点击
            const formulaContainer = formula.closest('.katex') || formula;

            // 添加视觉提示样式
            formulaContainer.style.cursor = 'pointer';
            formulaContainer.title = '点击复制公式';

            // 添加悬停效果
            formulaContainer.addEventListener('mouseenter', () => {
                formulaContainer.style.boxShadow = '0 0 3px 1px rgba(0, 161, 214, 0.5)';
            });

            formulaContainer.addEventListener('mouseleave', () => {
                formulaContainer.style.boxShadow = 'none';
            });

            // 添加点击事件
            formulaContainer.addEventListener('click', (event) => {
                event.preventDefault();
                event.stopPropagation();

                // 使用改进的公式提取方法
                let latexFormula = extractLatexFormula(formulaContainer);

                // 显示复制公式提示
                showFormulaPrompt(latexFormula, formulaContainer);

                // 增加使用计数
                incrementUsageCount();
            });
        });
    }

    // 改进的公式提取函数
    function extractLatexFormula(formulaContainer) {
        // 重置所有已处理标记
        formulaContainer.querySelectorAll('[data-processed]').forEach(el => {
            el.removeAttribute('data-processed');
        });

        // 首先尝试从annotation元素获取（这是最准确的来源）
        const annotation = formulaContainer.querySelector('.katex-mathml annotation');
        if (annotation) {
            return annotation.textContent;
        }

        // 如果找不到annotation，从HTML结构重建LaTeX
        const formula = formulaContainer.querySelector('.katex-html');
        if (!formula) return '';

        // 处理分式
        function processFraction(element) {
            const numerator = element.querySelector('.vlist-t:first-child .vlist-r:first-child .vlist > span:last-child');
            const denominator = element.querySelector('.vlist-t:first-child .vlist-r:first-child .vlist > span:first-child');

            if (!numerator || !denominator) return '';

            // 递归处理分子和分母
            const numText = processElement(numerator);
            const denText = processElement(denominator);

            return `\\frac{${numText}}{${denText}}`;
        }

        // 处理根号
        function processSqrt(element) {
            // 获取根号内容的容器
            const baseContent = element.querySelector('.vlist-t .vlist-r .vlist > span:last-child .vlist');
            if (!baseContent) {
                // 尝试其他选择器
                const altContent = element.querySelector('.sqrt-line + .vlist-t .vlist-r .vlist > span:last-child');
                if (!altContent) return '';
                return `\\sqrt{${processElement(altContent)}}`;
            }

            // 收集根号内所有内容
            let sqrtContent = '';
            const nodes = Array.from(baseContent.children);

            for (let i = 0; i < nodes.length; i++) {
                const node = nodes[i];
                if (!node) continue;

                // 处理基本元素
                if (node.classList.contains('mord') ||
                    node.classList.contains('mbin') ||
                    node.classList.contains('mrel') ||
                    node.classList.contains('mop')) {

                    // 处理上标
                    const sup = node.querySelector('.msupsub');
                    if (sup) {
                        const base = node.childNodes[0];
                        const power = sup.querySelector('.vlist-t .vlist-r .vlist > span:last-child');
                        if (base && power) {
                            sqrtContent += `${base.textContent}^{${power.textContent}}`;
                            continue;
                        }
                    }

                    // 处理普通文本
                    if (!node.children.length || node.children.length === 1) {
                        const text = node.textContent;
                        if (text === '±') sqrtContent += '\\pm';
                        else if (text === '×') sqrtContent += '\\times';
                        else if (text === '−') sqrtContent += '-';
                        else sqrtContent += text;
                        continue;
                    }
                }

                // 处理运算符
                if (node.classList.contains('mbin') || node.classList.contains('mrel')) {
                    sqrtContent += node.textContent;
                    continue;
                }

                // 递归处理其他元素
                const result = processElement(node);
                if (result) {
                    if (sqrtContent &&
                        /[a-zA-Z0-9]}]$/.test(sqrtContent) &&
                        /^[a-zA-Z0-9{]/.test(result)) {
                        sqrtContent += ' ';
                    }
                    sqrtContent += result;
                }
            }

            return `\\sqrt{${sqrtContent}}`;
        }

        // 处理上标
        function processSup(element) {
            const base = element.previousElementSibling;
            const sup = element.querySelector('.vlist-t .vlist-r .vlist > span:last-child');
            if (!base || !sup) return '';

            // 递归处理基数和指数
            const baseText = processElement(base);
            const supText = processElement(sup);

            // 检查是否需要添加括号
            const needBrackets = baseText.length > 1 && !baseText.match(/^[a-zA-Z0-9]$/);
            const formattedBase = needBrackets ? `{${baseText}}` : baseText;

            return `${formattedBase}^{${supText}}`;
        }

        // 处理下标
        function processSub(element) {
            const base = element.previousElementSibling;
            const sub = element.querySelector('.vlist-t .vlist-r .vlist > span:first-child');
            if (!base || !sub) return '';

            // 递归处理基数和下标
            const baseText = processElement(base);
            const subText = processElement(sub);

            return `${baseText}_{${subText}}`;
        }

        // 修改递归处理元素函数
        function processElement(element) {
            if (!element) return '';

            // 避免重复处理
            if (element.dataset.processed) return '';
            element.dataset.processed = 'true';

            // 处理不同类型的元素
            if (element.classList.contains('mfrac')) {
                return processFraction(element);
            }

            if (element.classList.contains('sqrt')) {
                return processSqrt(element);
            }

            // 处理上标和下标
            if (element.classList.contains('msupsub')) {
                const vlist = element.querySelector('.vlist-t .vlist-r .vlist');
                if (!vlist) return '';

                const spans = vlist.children;
                let result = '';

                // 检查是否有上标和下标
                let sup = null;
                let sub = null;

                for (const span of spans) {
                    if (span.style.top.includes('-2.55')) {
                        // 这是下标
                        sub = span.querySelector('.sizing');
                    } else if (span.style.top.includes('-3.063')) {
                        // 这是上标
                        sup = span.querySelector('.sizing');
                    }
                }

                // 获取基数
                const base = element.previousElementSibling;
                if (!base) return '';

                const baseText = processElement(base);

                // 添加上标和下标
                if (sup) {
                    result = `${baseText}^{${processElement(sup)}}`;
                }
                if (sub) {
                    result = result || baseText;
                    result += `_{${processElement(sub)}}`;
                }

                return result;
            }

            // 处理基本元素
            if (element.classList.contains('mord') ||
                element.classList.contains('mbin') ||
                element.classList.contains('mrel') ||
                element.classList.contains('mop')) {
                if (!element.children.length) {
                    // 处理特殊字符
                    const text = element.textContent;
                    if (text === '±') return '\\pm';
                    if (text === '×') return '\\times';
                    if (text === '÷') return '\\div';
                    if (text === '·') return '\\cdot';
                    if (text === '−') return '-';
                    return text;
                }
            }

            // 递归处理子元素
            let result = '';
            const children = Array.from(element.children);
            for (let i = 0; i < children.length; i++) {
                const child = children[i];
                const childResult = processElement(child);
                if (childResult) {
                    // 检查是否需要添加空格
                    if (i > 0 &&
                        /[a-zA-Z0-9]}]$/.test(result) &&
                        /^[a-zA-Z0-9{]/.test(childResult) &&
                        !child.classList.contains('msupsub')) {
                        result += ' ';
                    }
                    result += childResult;
                }
            }

            // 如果没有子元素但有文本内容
            if (!result && element.textContent) {
                result = element.textContent;
            }

            return result;
        }

        // 开始处理整个公式
        let result = processElement(formula);

        // 如果重建失败，返回基本文本
        if (!result) {
            result = formula.textContent.replace(/\s+/g, ' ').trim();
        }

        // 清理和格式化结果
        return formatLatexFormula(result);
    }

    // 格式化LaTeX公式
    function formatLatexFormula(formula) {
        return formula
            // 修复可能的语法问题
            .replace(/([a-zA-Z0-9])\\/g, '$1 \\')
            // 处理连续的负号
            .replace(/--/g, '-')
            // 修复特殊命令后的空格
            .replace(/\\(times|pm|div|cdot)(?=[a-zA-Z])/g, '\\$1 ')
            // 修复运算符周围的空格
            .replace(/\s*([=+\-*/±])\s*/g, ' $1 ')
            // 修复括号周围的空格
            .replace(/\s*([{}()])\s*/g, '$1')
            // 修复根号内的空格
            .replace(/\\sqrt\{\s+/g, '\\sqrt{')
            .replace(/\s+\}/g, '}')
            // 修复上标和下标格式
            .replace(/\^{(\d+)}/g, '^{$1}')
            .replace(/_{(\d+)}/g, '_{$1}')
            // 修复多余的空格
            .replace(/\s+/g, ' ')
            .trim();
    }

    // 显示公式复制提示
    function showFormulaPrompt(formula, sourceElement) {
        // 检查是否已存在提示框
        if (document.querySelector('.formula-prompt')) {
            document.body.removeChild(document.querySelector('.formula-prompt'));
        }

        // 创建提示框
        const prompt = document.createElement('div');
        prompt.className = 'formula-prompt';
        prompt.style.cssText = `
            position: fixed;
            bottom: 20px;
            right: 20px;
            background-color: rgba(34, 34, 34, 0.9);
            color: white;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
            z-index: 10000;
            font-size: 14px;
            max-width: 300px;
            transition: opacity 0.3s;
        `;

        // 提示框内容
        prompt.innerHTML = `
            <div style="margin-bottom: 10px; font-weight: bold;">数学公式</div>
            <div style="margin-bottom: 12px; color: #ddd; font-size: 12px; word-break: break-all;
                        background: rgba(0,0,0,0.2); padding: 8px; border-radius: 4px;
                        font-family: monospace; overflow-x: auto;">${formula}</div>
            <div style="display: flex; gap: 10px;">
                <button class="copy-latex" style="
                    background-color: #00a1d6;
                    color: white;
                    border: none;
                    padding: 5px 10px;
                    border-radius: 4px;
                    cursor: pointer;
                    flex: 1;
                ">复制LaTeX</button>
                <button class="copy-text" style="
                    background-color: #4CAF50;
                    color: white;
                    border: none;
                    padding: 5px 10px;
                    border-radius: 4px;
                    cursor: pointer;
                    flex: 1;
                ">复制文本</button>
                <button class="close-prompt" style="
                    background-color: transparent;
                    color: #ddd;
                    border: 1px solid #666;
                    padding: 5px 10px;
                    border-radius: 4px;
                    cursor: pointer;
                ">关闭</button>
            </div>
        `;

        // 添加到页面
        document.body.appendChild(prompt);

        // 复制LaTeX按钮点击事件
        prompt.querySelector('.copy-latex').addEventListener('click', function() {
            GM_setClipboard(formula);
            showNotification('已复制LaTeX公式');
            document.body.removeChild(prompt);
        });

        // 复制文本按钮点击事件
        prompt.querySelector('.copy-text').addEventListener('click', function() {
            let textFormula = formula
                // 处理分式
                .replace(/\\frac\{([^{}]+)\}\{([^{}]+)\}/g, '($1)/($2)')
                // 处理上标
                .replace(/\^{([^{}]+)}/g, '^($1)')
                .replace(/\^(\d+)(?![)}])/g, '^($1)')
                // 处理下标
                .replace(/_{([^{}]+)}/g, '_($1)')
                .replace(/_(\d+)(?![)}])/g, '_($1)')
                // 处理根号
                .replace(/\\sqrt\{([^{}]+)\}/g, 'sqrt($1)')
                // 处理特殊符号（确保添加空格）
                .replace(/\\times(?!\s)/g, '* ')
                .replace(/\\pm(?!\s)/g, '± ')
                .replace(/\\div(?!\s)/g, '/ ')
                .replace(/\\cdot(?!\s)/g, '* ')
                // 处理希腊字母
                .replace(/\\(alpha|beta|gamma|delta|epsilon|theta|pi|sigma|omega)/g, '\\$1')
                // 保持运算符周围的空格
                .replace(/([a-zA-Z0-9])([\+\-\*\/=±])/g, '$1 $2')
                .replace(/([\+\-\*\/=±])([a-zA-Z0-9])/g, '$1 $2')
                // 清理多余的空格和括号
                .replace(/\(\s+/g, '(')
                .replace(/\s+\)/g, ')')
                .replace(/\s+/g, ' ')
                .trim();

            GM_setClipboard(textFormula);
            showNotification('已复制文本形式');
            document.body.removeChild(prompt);
        });

        // 关闭按钮点击事件
        prompt.querySelector('.close-prompt').addEventListener('click', function() {
            document.body.removeChild(prompt);
        });

        // 10秒后自动关闭
        setTimeout(() => {
            if (document.body.contains(prompt)) {
                prompt.style.opacity = '0';
                setTimeout(() => {
                    if (document.body.contains(prompt)) {
                        document.body.removeChild(prompt);
                    }
                }, 300);
            }
        }, 10000);
    }

    // 显示信息遮罩层
    function showInfoOverlay() {
        let apiData = null; // Variable to store API response

        // 检查是否已存在卡片
        if (document.querySelector('.info-overlay')) {
            return;
        }

        // 创建卡片元素
        const overlay = document.createElement('div');
        overlay.className = 'info-overlay';
        overlay.style.cssText = `
            position: fixed;
            top: 0;
            left: 0;
            width: 100vw;
            height: 100vh;
            background-color: rgba(0, 0, 0, 0.85); /* 半透明黑色背景 */
            color: white;
            padding: 20px;
            z-index: 10002; /* 比其他UI高 */
            display: flex;
            justify-content: center;
            align-items: center;
            backdrop-filter: blur(5px); /* 背景模糊效果 */
            transition: opacity 0.3s ease-in-out;
        `;

        // 创建内容容器
        const contentContainer = document.createElement('div');
        contentContainer.style.cssText = `
            background: linear-gradient(135deg, rgba(30, 30, 30, 0.95) 0%, rgba(20, 20, 20, 0.98) 100%);
            padding: 35px;
            border-radius: 15px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
            position: relative;
            width: 95%;
            height: 95vh;
            max-height: 90vh;
            overflow-y: auto;
            font-size: 14px;
            text-align: center;
            display: flex;
            flex-direction: column;
            border: 1px solid rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
        `;

        // Interval ID for dynamic update
        let intervalId = null;
        let resizeListener = null;

        // 更新时间和视口大小的函数
        function updateDynamicInfo() {
            const now = new Date();
            // 格式化日期时间 (YYYY-MM-DD HH:MM:SS)
            const year = now.getFullYear();
            const month = (now.getMonth() + 1).toString().padStart(2, '0');
            const day = now.getDate().toString().padStart(2, '0');
            const hours = now.getHours().toString().padStart(2, '0');
            const minutes = now.getMinutes().toString().padStart(2, '0');
            const seconds = now.getSeconds().toString().padStart(2, '0');
            const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

            const timeElement = overlay.querySelector('.info-time');
            if (timeElement) {
                timeElement.textContent = formattedDateTime;
            }

            const viewportElement = overlay.querySelector('.info-viewport');
            if (viewportElement) {
                viewportElement.textContent = `${window.innerWidth} x ${window.innerHeight}`;
            }
        }

        // 获取设置和其他静态信息
        const settings = getSettings();
        const installDate = GM_getValue('installDate', Date.now());
        const daysUsed = Math.ceil((Date.now() - installDate) / (1000 * 60 * 60 * 24));

        // 构建HTML内容
        contentContainer.innerHTML = `
            <h2 style="margin-top: 0; margin-bottom: 20px; color: #4fc3f7; font-size: 28px; text-shadow: 0 2px 4px rgba(0,0,0,0.5); font-weight: 300;">
                信息面板
                <span class="info-time" style="margin-left: 15px; font-size: 24px; color: #81c784; font-family: 'Courier New', monospace; background: rgba(0,0,0,0.2); padding: 4px 12px; border-radius: 8px;"></span>
            </h2>

            <!-- 日历卡片 -->
            <div style="display: flex; justify-content: center; margin-bottom: 25px;">
                <div style="width: 220px; height: 280px; background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%); border-radius: 15px; box-shadow: 0 8px 24px rgba(0,0,0,0.15); overflow: hidden; color: #333; transition: transform 0.3s ease;">
                    <!-- 红色顶部 -->
                    <div style="background: linear-gradient(135deg, #f44336 0%, #d32f2f 100%); color: white; text-align: center; padding: 12px 0; font-size: 18px; font-weight: 500;" class="calendar-header">
                        <span class="calendar-year">2025</span>年<span class="calendar-month">4</span>月
                    </div>

                    <!-- 大日期数字 -->
                    <div style="font-size: 96px; font-weight: 700; text-align: center; line-height: 1.1; padding: 25px 0 15px; font-family: 'Arial', sans-serif; background: linear-gradient(45deg, #333 0%, #666 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent;" class="calendar-day">
                        26
                    </div>

                    <!-- 年份日期信息 -->
                    <div style="text-align: center; font-size: 13px; color: #666; padding: 8px 0; background: rgba(0,0,0,0.03);">
                        第<span class="calendar-day-of-year">116</span>天 第<span class="calendar-week">17</span>周
                    </div>

                    <!-- 农历信息 -->
                    <div style="text-align: center; font-size: 16px; padding: 12px 0; color: #333; background: linear-gradient(to bottom, #fff 0%, #f8f9fa 100%);" class="calendar-lunar-container">
                        <span class="calendar-lunar" style="font-weight: 600;">加载中...</span>
                        <span class="calendar-weekday" style="color: #666;">加载中...</span>
                    </div>

                    <!-- 旧历标签 -->
                    <div style="text-align: center; background: linear-gradient(to bottom, #f5f5f5 0%, #eee 100%); padding: 6px 0; font-size: 12px; color: #888;">
                        旧历
                    </div>
                </div>
            </div>

            <!-- 中间：农历日期信息 -->
            <div style="display: flex; background: linear-gradient(135deg, rgba(25, 25, 25, 0.6) 0%, rgba(35, 35, 35, 0.6) 100%); border-radius: 12px; padding: 25px; margin-bottom: 25px; justify-content: space-around; align-items: center; border: 1px solid rgba(255, 255, 255, 0.1); backdrop-filter: blur(5px);">
                <div style="text-align: center; padding: 0 20px; position: relative;">
                    <div style="font-size: 18px; color: #bdbdbd; margin-bottom: 8px; font-weight: 300;">农历</div>
                    <div class="info-lunar-date" style="font-size: 32px; color: #ffeb3b; text-shadow: 0 2px 4px rgba(0,0,0,0.2);">加载中...</div>
                </div>
                <div style="text-align: center; padding: 0 20px; position: relative;">
                    <div style="font-size: 18px; color: #bdbdbd; margin-bottom: 8px; font-weight: 300;">干支</div>
                    <div class="info-ganzhi" style="font-size: 24px; color: #81c784; text-shadow: 0 2px 4px rgba(0,0,0,0.2);">加载中...</div>
                </div>
                <div style="text-align: center; padding: 0 20px; position: relative;">
                    <div style="font-size: 18px; color: #bdbdbd; margin-bottom: 8px; font-weight: 300;">节气</div>
                    <div class="info-jieqi" style="font-size: 32px; color: #64b5f6; text-shadow: 0 2px 4px rgba(0,0,0,0.2);">加载中...</div>
                </div>
                <div style="text-align: center; padding: 0 20px; position: relative;">
                    <div style="font-size: 18px; color: #bdbdbd; margin-bottom: 8px; font-weight: 300;">冲煞</div>
                    <div class="info-chongsha" style="font-size: 24px; color: #e57373; text-shadow: 0 2px 4px rgba(0,0,0,0.2);">加载中...</div>
                </div>
            </div>

            <!-- 底部：节气、宜忌信息 -->
            <div style="display: flex; flex-direction: row; gap: 15px;">
                <!-- 左下：节气和节日 -->
                <div style="flex: 1; background: linear-gradient(135deg, rgba(25, 25, 25, 0.6) 0%, rgba(35, 35, 35, 0.6) 100%); border-radius: 12px; padding: 20px; text-align: left; border: 1px solid rgba(255, 255, 255, 0.1); backdrop-filter: blur(5px);">
                    <h3 style="margin-top: 0; color: #64b5f6; margin-bottom: 20px; border-bottom: 1px solid rgba(255, 255, 255, 0.1); padding-bottom: 12px; font-weight: 300;">
                        <span style="font-size: 18px; vertical-align: middle;">🌿</span> 节气与节日
                    </h3>
                    <div style="display: grid; grid-template-columns: auto 1fr; gap: 12px 15px; line-height: 1.6;">
                        <strong style="color: #9e9e9e; font-weight: 500;">节气信息:</strong>
                        <span class="info-jieqi" style="color: #7986cb; background: rgba(0,0,0,0.2); padding: 4px 12px; border-radius: 6px;">加载中...</span>
                        <strong style="color: #9e9e9e; font-weight: 500;">节日事件:</strong>
                        <span class="info-jieri" style="color: #ffcc80; background: rgba(0,0,0,0.2); padding: 4px 12px; border-radius: 6px;">加载中...</span>
                        <strong style="color: #9e9e9e; font-weight: 500;">彭祖百忌:</strong>
                        <span class="info-pengzu" style="color: #b39ddb; background: rgba(0,0,0,0.2); padding: 4px 12px; border-radius: 6px; font-size: 0.9em;">加载中...</span>
                    </div>
                </div>

                <!-- 右下：宜忌 -->
                <div style="flex: 1; background: linear-gradient(135deg, rgba(25, 25, 25, 0.6) 0%, rgba(35, 35, 35, 0.6) 100%); border-radius: 12px; padding: 20px; text-align: left; border: 1px solid rgba(255, 255, 255, 0.1); backdrop-filter: blur(5px);">
                    <h3 style="margin-top: 0; color: #64b5f6; margin-bottom: 20px; border-bottom: 1px solid rgba(255, 255, 255, 0.1); padding-bottom: 12px; font-weight: 300;">
                        <span style="font-size: 18px; vertical-align: middle;">📅</span> 今日宜忌
                    </h3>
                    <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px;">
                        <div style="background: linear-gradient(135deg, rgba(38, 77, 0, 0.2) 0%, rgba(38, 77, 0, 0.1) 100%); padding: 15px; border-radius: 8px; border-left: 3px solid #81c784; backdrop-filter: blur(5px);">
                            <strong style="color: #81c784; display: block; margin-bottom: 8px; font-size: 15px;">● 宜</strong>
                            <div class="info-yi" style="color: #a5d6a7; word-break: break-word; line-height: 1.6; font-size: 13px;">加载中...</div>
                        </div>
                        <div style="background: linear-gradient(135deg, rgba(77, 0, 0, 0.2) 0%, rgba(77, 0, 0, 0.1) 100%); padding: 15px; border-radius: 8px; border-left: 3px solid #e57373; backdrop-filter: blur(5px);">
                            <strong style="color: #e57373; display: block; margin-bottom: 8px; font-size: 15px;">● 忌</strong>
                            <div class="info-ji" style="color: #ef9a9a; word-break: break-word; line-height: 1.6; font-size: 13px;">加载中...</div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 调试信息按钮 -->
            <div style="text-align: center; margin-top: 35px;">
                <button class="debug-info-toggle" style="
                    background: linear-gradient(135deg, rgba(30, 30, 30, 0.7) 0%, rgba(40, 40, 40, 0.7) 100%);
                    color: #757575;
                    border: 1px solid rgba(255, 255, 255, 0.1);
                    border-radius: 8px;
                    padding: 10px 20px;
                    font-size: 13px;
                    cursor: pointer;
                    transition: all 0.3s ease;
                    backdrop-filter: blur(5px);
                ">显示调试信息</button>
            </div>

            <!-- 调试信息面板（默认隐藏） -->
            <div class="debug-info-panel" style="display: none; margin-top: 20px;">
                <div style="display: flex; flex-direction: row; gap: 10px; margin-bottom: 20px;">
                    <!-- 左侧：系统信息 -->
                    <div style="flex: 1; background: rgba(20, 20, 20, 0.3); border-radius: 5px; padding: 15px; text-align: left;">
                        <h3 style="margin-top: 0; color: #64b5f6; margin-bottom: 15px; border-bottom: 1px solid #444; padding-bottom: 8px;">
                            <span style="font-size: 16px; vertical-align: middle;">💻</span> 系统信息
                        </h3>
                        <div style="display: grid; grid-template-columns: auto 1fr; gap: 8px 10px; line-height: 1.5;">
                            <strong style="color: #9e9e9e;">浏览器语言:</strong> <span>${navigator.language}</span>
                            <strong style="color: #9e9e9e;">操作系统:</strong> <span>${navigator.platform}</span>
                            <strong style="color: #9e9e9e;">屏幕分辨率:</strong> <span>${screen.width} x ${screen.height}</span>
                            <strong style="color: #9e9e9e;">浏览器视口:</strong> <span class="info-viewport" style="font-family: monospace;"></span>
                            <strong style="color: #9e9e9e;">脚本总使用:</strong> <span style="color: #ffb74d;">${settings.usageCount} 次</span>
                            <strong style="color: #9e9e9e;">脚本已使用:</strong> <span style="color: #ffb74d;">${daysUsed} 天</span>
                        </div>
                    </div>

                    <!-- 右侧：页面信息 -->
                    <div style="flex: 1; background: rgba(20, 20, 20, 0.3); border-radius: 5px; padding: 15px; text-align: left;">
                        <h3 style="margin-top: 0; color: #64b5f6; margin-bottom: 15px; border-bottom: 1px solid #444; padding-bottom: 8px;">
                            <span style="font-size: 16px; vertical-align: middle;">🌐</span> 页面信息
                        </h3>
                        <div style="line-height: 1.5;">
                            <div style="margin-bottom: 8px;">
                                <strong style="color: #9e9e9e; display: block; margin-bottom: 2px;">页面标题:</strong>
                                <div style="word-break: break-all; max-height: 60px; overflow-y: auto; padding: 5px; background: rgba(0,0,0,0.2); border-radius: 3px;">${document.title}</div>
                            </div>
                            <div>
                                <strong style="color: #9e9e9e; display: block; margin-bottom: 2px;">页面URL:</strong>
                                <div style="word-break: break-all; max-height: 60px; overflow-y: auto; padding: 5px; background: rgba(0,0,0,0.2); border-radius: 3px;">
                                    <a href="${window.location.href}" target="_blank" style="color: #4fc3f7;">${window.location.href}</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- API 响应数据 -->
                <div style="background: rgba(20, 20, 20, 0.3); border-radius: 5px; padding: 15px; text-align: left; margin-bottom: 10px;">
                    <h3 style="margin-top: 0; color: #64b5f6; margin-bottom: 15px; border-bottom: 1px solid #444; padding-bottom: 8px;">
                        <span style="font-size: 16px; vertical-align: middle;">🔄</span> API 响应数据
                    </h3>
                    <pre class="api-response-data" style="
                        background: rgba(0, 0, 0, 0.2);
                        padding: 10px;
                        border-radius: 4px;
                        color: #aaa;
                        font-family: monospace;
                        font-size: 12px;
                        max-height: 200px;
                        overflow-y: auto;
                        white-space: pre-wrap;
                        word-break: break-all;
                    ">加载中...</pre>
                </div>
            </div>
        `;

        // 创建关闭按钮
        const closeButton = document.createElement('button');
        closeButton.innerHTML = '&times;'; // HTML entity for 'X'
        closeButton.style.cssText = `
            position: absolute;
            top: 15px;
            right: 15px;
            background: transparent;
            border: none;
            z-index: 10003; /* Ensure button is above content */
            color: #aaa;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
            padding: 0 10px;
            line-height: 1;
            transition: color 0.2s;
        `;
        closeButton.onmouseover = () => closeButton.style.color = '#fff';
        closeButton.onmouseout = () => closeButton.style.color = '#aaa';

        // 清理函数
        function closeOverlay() {
            clearInterval(intervalId);
            window.removeEventListener('resize', resizeListener);
            document.body.removeChild(overlay);
        }

        // 为关闭按钮添加事件
        closeButton.addEventListener('click', closeOverlay);

        // Function to update API data display
        function updateApiDisplay(data) {
            // 为所有元素添加空值检查，避免尝试设置不存在元素的 textContent 属性
            const lunarDateEl = overlay.querySelector('.info-lunar-date');
            if (lunarDateEl) lunarDateEl.textContent = `${data.nyue}${data.nri}`;

            const calendarLunarEl = overlay.querySelector('.calendar-lunar');
            if (calendarLunarEl) calendarLunarEl.textContent = `${data.nyue}${data.nri}`;

            const weekdayEl = overlay.querySelector('.calendar-weekday');
            if (weekdayEl) weekdayEl.textContent = data.xingqi;

            const ganzhiEl = overlay.querySelector('.info-ganzhi');
            if (ganzhiEl) ganzhiEl.textContent = `${data.ganzhinian} ${data.ganzhiyue} ${data.ganzhiri}`;

            const xingqiEl = overlay.querySelector('.info-xingqi');
            if (xingqiEl) xingqiEl.textContent = `${data.xingqi}`;

            // 获取节气信息 - 同时处理中间部分和左下方的节气信息
            let jieqiInfo = '无节气';
            if (data.jieqimsg && data.jieqimsg.trim() !== '') {
                jieqiInfo = data.jieqimsg;
            } else if (data.jieqi && data.jieqi.trim() !== '') {
                jieqiInfo = data.jieqi;
            }

            // 更新所有节气相关元素
            const jieqiElements = overlay.querySelectorAll('.info-jieqi');
            jieqiElements.forEach(element => {
                if (element) element.textContent = jieqiInfo;
            });

            // 节日信息 - 同样处理多处显示
            let jieriInfo = '无节日';
            if (data.jieri && data.jieri.trim() !== '') {
                jieriInfo = data.jieri;
            }

            const jieriElements = overlay.querySelectorAll('.info-jieri');
            jieriElements.forEach(element => {
                if (element) element.textContent = jieriInfo;
            });

            const chongshaEl = overlay.querySelector('.info-chongsha');
            if (chongshaEl) chongshaEl.textContent = data.xiangchong || '无';

            const yiEl = overlay.querySelector('.info-yi');
            if (yiEl) yiEl.textContent = data.yi || '无';

            const jiEl = overlay.querySelector('.info-ji');
            if (jiEl) jiEl.textContent = data.ji || '无';

            const pengzuEl = overlay.querySelector('.info-pengzu');
            if (pengzuEl) pengzuEl.textContent = data.pengzu || '无';

            // Update calendar view
            const today = new Date();
            const yearEl = overlay.querySelector('.calendar-year');
            if (yearEl) yearEl.textContent = data.ynian;

            const monthEl = overlay.querySelector('.calendar-month');
            if (monthEl) monthEl.textContent = data.yyue;

            const dayEl = overlay.querySelector('.calendar-day');
            if (dayEl) dayEl.textContent = data.yri;

            // Calculate day of year
            const startOfYear = new Date(today.getFullYear(), 0, 0);
            const diff = today - startOfYear;
            const oneDay = 1000 * 60 * 60 * 24;
            const dayOfYear = Math.floor(diff / oneDay);

            const dayOfYearEl = overlay.querySelector('.calendar-day-of-year');
            if (dayOfYearEl) dayOfYearEl.textContent = dayOfYear;

            // Get week number
            const weekNumber = Math.ceil(dayOfYear / 7);
            const weekEl = overlay.querySelector('.calendar-week');
            if (weekEl) weekEl.textContent = weekNumber;

            // 将API响应数据面板改为生成图片显示（而不是JSON代码块）
            const apiResponseEl = overlay.querySelector('.api-response-data');
            if (apiResponseEl) {
                // 清空原来的内容
                apiResponseEl.innerHTML = '';

                try {
                    // 使用HTML和CSS方式显示，而不是Canvas
                    const infoContainer = document.createElement('div');
                    infoContainer.className = 'api-info-container';
                    infoContainer.style.cssText = `
                        width: 100%;
                        padding: 15px;
                        background-color: #f8f9fa;
                        border: 1px solid #ddd;
                        border-radius: 4px;
                        position: relative;
                        font-family: Arial, sans-serif;
                        overflow: hidden;
                    `;

                    // 添加水印背景
                    const watermarkContainer = document.createElement('div');
                    watermarkContainer.style.cssText = `
                        position: absolute;
                        top: 0;
                        left: 0;
                        width: 100%;
                        height: 100%;
                        z-index: 0;
                        pointer-events: none;
                        overflow: hidden;
                    `;

                    // 生成重复的水印
                    for (let y = -20; y < 300; y += 60) {
                        for (let x = -50; x < 400; x += 150) {
                            const watermark = document.createElement('div');
                            watermark.textContent = '仅供调试使用';
                            watermark.style.cssText = `
                                position: absolute;
                                left: ${x}px;
                                top: ${y}px;
                                transform: rotate(-45deg);
                                font-size: 14px;
                                color: rgba(200, 200, 200, 0.6);
                                white-space: nowrap;
                            `;
                            watermarkContainer.appendChild(watermark);
                        }
                    }
                    infoContainer.appendChild(watermarkContainer);

                    // 添加标题
                    const title = document.createElement('h3');
                    title.textContent = '系统信息';
                    title.style.cssText = `
                        margin-top: 0;
                        margin-bottom: 15px;
                        font-size: 16px;
                        color: #333;
                        position: relative;
                        z-index: 1;
                    `;
                    infoContainer.appendChild(title);

                    // 创建信息表格
                    const infoTable = document.createElement('table');
                    infoTable.style.cssText = `
                        width: 100%;
                        border-collapse: collapse;
                        position: relative;
                        z-index: 1;
                        font-size: 13px;
                    `;

                    // 显示简化的系统信息和页面信息
                    const keys = [
                        '日期', '农历', '干支', '节气',
                        '节日', '冲煞', '宜', '忌'
                    ];

                    // 获取节气信息
                    let jieqiInfo = '无节气';
                    if (data.jieqimsg && data.jieqimsg.trim() !== '') {
                        jieqiInfo = data.jieqimsg;
                    } else if (data.jieqi && data.jieqi.trim() !== '') {
                        jieqiInfo = data.jieqi;
                    }

                    // 获取节日信息
                    let jieriInfo = '无节日';
                    if (data.jieri && data.jieri.trim() !== '') {
                        jieriInfo = data.jieri;
                    }

                    const values = [
                        `${data.ynian}-${data.yyue}-${data.yri} ${data.xingqi}`,
                        `${data.nyue}${data.nri}`,
                        `${data.ganzhinian} ${data.ganzhiyue} ${data.ganzhiri}`,
                        jieqiInfo,
                        jieriInfo,
                        data.xiangchong || '无',
                        data.yi || '无',
                        data.ji || '无'
                    ];

                    for (let i = 0; i < keys.length; i++) {
                        const row = document.createElement('tr');

                        const keyCell = document.createElement('td');
                        keyCell.textContent = keys[i] + ':';
                        keyCell.style.cssText = `
                            padding: 5px;
                            font-weight: bold;
                            width: 15%;
                            vertical-align: top;
                            color: #555;
                        `;

                        const valueCell = document.createElement('td');
                        valueCell.textContent = values[i];
                        valueCell.style.cssText = `
                            padding: 5px;
                            color: #333;
                            word-break: break-word;
                        `;

                        row.appendChild(keyCell);
                        row.appendChild(valueCell);
                        infoTable.appendChild(row);
                    }

                    infoContainer.appendChild(infoTable);
                    apiResponseEl.appendChild(infoContainer);

                } catch (e) {
                    console.error('Error generating info display:', e);
                    // 如果生成显示失败，回退到简单文本
                    apiResponseEl.innerHTML = '<div style="color:#555;padding:10px;">数据已加载 (显示受限)</div>';
                }
            }
        }

        // Function to set API display to loading error
        function setApiLoadingError() {
            const errorMsg = '加载失败';

            // 为所有元素添加空值检查
            const elementsToUpdate = [
                '.info-lunar-date', '.calendar-lunar', '.calendar-weekday',
                '.info-ganzhi', '.info-xingqi', '.info-pengzu'
            ];

            elementsToUpdate.forEach(selector => {
                const element = overlay.querySelector(selector);
                if (element) element.textContent = errorMsg;
            });

            // 单独处理节气相关元素 - 确保所有节气显示都更新
            const jieqiElements = overlay.querySelectorAll('.info-jieqi');
            jieqiElements.forEach(element => {
                if (element) element.textContent = errorMsg;
            });

            // 单独处理节日相关元素
            const jieriElements = overlay.querySelectorAll('.info-jieri');
            jieriElements.forEach(element => {
                if (element) element.textContent = errorMsg;
            });

            // 更新其他元素
            const chongshaEl = overlay.querySelector('.info-chongsha');
            if (chongshaEl) chongshaEl.textContent = errorMsg;

            const yiEl = overlay.querySelector('.info-yi');
            if (yiEl) yiEl.textContent = errorMsg;

            const jiEl = overlay.querySelector('.info-ji');
            if (jiEl) jiEl.textContent = errorMsg;

            const pengzuEl = overlay.querySelector('.info-pengzu');
            if (pengzuEl) pengzuEl.textContent = errorMsg;

            // 显示API错误信息面板
            const apiResponseEl = overlay.querySelector('.api-response-data');
            if (apiResponseEl) {
                try {
                    // 使用HTML和CSS显示错误信息
                    apiResponseEl.innerHTML = '';

                    const errorContainer = document.createElement('div');
                    errorContainer.style.cssText = `
                        width: 100%;
                        padding: 15px;
                        background-color: #fff0f0;
                        border: 1px solid #ffcccc;
                        border-radius: 4px;
                        position: relative;
                        font-family: Arial, sans-serif;
                        overflow: hidden;
                    `;

                    // 添加水印背景
                    const watermarkContainer = document.createElement('div');
                    watermarkContainer.style.cssText = `
                        position: absolute;
                        top: 0;
                        left: 0;
                        width: 100%;
                        height: 100%;
                        z-index: 0;
                        pointer-events: none;
                        overflow: hidden;
                    `;

                    // 生成重复的水印
                    for (let y = -20; y < 300; y += 60) {
                        for (let x = -50; x < 400; x += 150) {
                            const watermark = document.createElement('div');
                            watermark.textContent = '仅供调试使用';
                            watermark.style.cssText = `
                                position: absolute;
                                left: ${x}px;
                                top: ${y}px;
                                transform: rotate(-45deg);
                                font-size: 14px;
                                color: rgba(200, 200, 200, 0.6);
                                white-space: nowrap;
                            `;
                            watermarkContainer.appendChild(watermark);
                        }
                    }
                    errorContainer.appendChild(watermarkContainer);

                    // 错误标题
                    const errorTitle = document.createElement('div');
                    errorTitle.textContent = 'API请求失败';
                    errorTitle.style.cssText = `
                        color: #cc0000;
                        font-weight: bold;
                        font-size: 16px;
                        margin-bottom: 10px;
                        position: relative;
                        z-index: 1;
                    `;
                    errorContainer.appendChild(errorTitle);

                    // 错误信息
                    const errorMessage = document.createElement('div');
                    errorMessage.textContent = '无法获取数据，请稍后再试';
                    errorMessage.style.cssText = `
                        color: #333;
                        font-size: 14px;
                        position: relative;
                        z-index: 1;
                    `;
                    errorContainer.appendChild(errorMessage);

                    apiResponseEl.appendChild(errorContainer);
                } catch (e) {
                    console.error('Error generating error display:', e);
                    // 如果失败，回退到简单文本
                    apiResponseEl.innerHTML = '<div style="color:#c00;padding:10px;">API请求失败</div>';
                }
            }
        }

        // 为显示/隐藏调试信息按钮添加事件
        // 使用setTimeout确保DOM完全加载后再绑定事件
        setTimeout(() => {
            const debugToggleBtn = overlay.querySelector('.debug-info-toggle');
            const debugPanel = overlay.querySelector('.debug-info-panel');

            if (debugToggleBtn && debugPanel) {
                debugToggleBtn.addEventListener('click', function() {
                    const isVisible = debugPanel.style.display !== 'none';
                    debugPanel.style.display = isVisible ? 'none' : 'block';
                    debugToggleBtn.textContent = isVisible ? '显示调试信息' : '隐藏调试信息';
                    debugToggleBtn.style.color = isVisible ? '#757575' : '#4fc3f7';

                    // 如果显示调试面板，则滚动到底部
                    if (!isVisible) {
                        setTimeout(() => {
                            contentContainer.scrollTo({
                                top: contentContainer.scrollHeight,
                                behavior: 'smooth'
                            });
                        }, 100);
                    }
                });
            } else {
                console.error('调试按钮或面板元素未找到');
            }
        }, 0);

        // 组合元素
        overlay.appendChild(contentContainer);
        contentContainer.appendChild(closeButton); // Append button to the content card

        // 添加到页面
        document.body.appendChild(overlay);

        // Fetch data from API
        // 原始直接请求的API URL
        // const apiUrl = 'https://cn.apihz.cn/api/time/getday.php?id=10000687&key=3cfab27db919e2fa3cf8075df8f04732';

        // 替换为通过代理脚本请求
        const apiUrl = 'https://api.mg-tool.cn/js'; // 使用已部署的代理脚本URL

        // 生成验证头
        function generateAuthToken() {
            // 客户端秘钥 - 这段代码主要用于提高破解门槛
            const secretKey = 'mg_tool_api_secret_key_' + new Date().getFullYear();

            // 获取当前时间戳
            const timestamp = Math.floor(Date.now() / 1000).toString();

            // 生成HMAC签名 (在用户脚本中模拟)
            function hmacSha256(message, key) {
                // 复杂的签名算法，增加破解难度
                let result = '';
                const salt = 'x#9&p!2@z';
                for (let i = 0; i < message.length; i++) {
                    const charCode = message.charCodeAt(i) ^ key.charCodeAt(i % key.length);
                    const saltChar = salt.charCodeAt(i % salt.length);
                    result += (charCode ^ saltChar).toString(16).padStart(2, '0');
                }
                return result + timestamp.split('').reverse().join('');
            }

            const signature = hmacSha256(timestamp, secretKey);

            // 返回格式：timestamp.signature
            return `${timestamp}.${signature}`;
        }

        // 延迟发送API请求，确保DOM元素已经完全加载
        setTimeout(() => {
            GM_xmlhttpRequest({
                method: 'GET',
                url: apiUrl,
                headers: {
                    'X-Auth-Token': generateAuthToken()
                },
                onload: function(response) {
                    try {
                        apiData = JSON.parse(response.responseText);
                        if (apiData.code === 200) {
                            updateApiDisplay(apiData);
                        } else {
                            console.error('API Error:', apiData);
                            setApiLoadingError();
                        }
                    } catch (e) {
                        console.error('Error parsing API response:', e);
                        setApiLoadingError();
                    }
                },
                onerror: function(error) {
                    console.error('GM_xmlhttpRequest Error:', error);
                    setApiLoadingError();
                }
            });
        }, 500); // 延迟500毫秒发送请求

        // Initial time update
        updateDynamicInfo();

        // Start dynamic update interval
        intervalId = setInterval(updateDynamicInfo, 1000);

        // Add resize listener for viewport size
        resizeListener = updateDynamicInfo; // Assign function directly
        window.addEventListener('resize', resizeListener);

        // 初始加载文本
        overlay.querySelector('.info-lunar-date').textContent = '加载中...';
        overlay.querySelector('.calendar-lunar').textContent = '加载中...';
        overlay.querySelector('.calendar-weekday').textContent = '加载中...';
        overlay.querySelector('.info-ganzhi').textContent = '加载中...';
        overlay.querySelector('.info-xingqi').textContent = '加载中...';
        overlay.querySelector('.info-jieqi').textContent = '加载中...';
        overlay.querySelector('.info-jieri').textContent = '加载中...';
        overlay.querySelector('.info-chongsha').textContent = '加载中...';
        overlay.querySelector('.info-yi').textContent = '加载中...';
        overlay.querySelector('.info-ji').textContent = '加载中...';
        overlay.querySelector('.info-pengzu').textContent = '加载中...';
    }

    init();
})();